/*******************************************************************************
 * Copyright (c) 2011 LegSem.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.txt
 * 
 * Contributors:
 *     LegSem - initial API and implementation
 ******************************************************************************/
package com.legstar.eci.xmi;

import java.util.Map;

import javax.xml.stream.XMLStreamReader;

import com.thoughtworks.xstream.io.xml.AbstractPullReader;
import com.thoughtworks.xstream.io.xml.QNameMap;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;

/**
 * A slight enhancement to the XStream StaxDriver to support attributes
 * with namespace prefixes.
 *
 */
public class AttrStaxDriver extends StaxDriver {

    /** Maps classeName#fieldName to attribute namespace uri. */
	private Map < String, String > attrToNamespaceUri;

	/**
	 * @param qnameMap maps a class to a qualified name
	 * @param attrToNamespaceUri maps a field from a class to a namespace URI
	 * @param replacer an XML name beautifier
	 */
	public AttrStaxDriver(
			QNameMap qnameMap,
			Map < String, String > attrToNamespaceUri,
			XmlFriendlyReplacer replacer) {
		super(qnameMap, replacer);
		this.attrToNamespaceUri = attrToNamespaceUri;
	}

	/**
	 * @param qnameMap maps a class to a qualified name
	 * @param attrToNamespaceUri maps a field from a class to a namespace URI
	 */
	public AttrStaxDriver(
			QNameMap qnameMap,
			Map < String, String > attrToNamespaceUri) {
		super(qnameMap);
        //setRepairingNamespace(true);
		this.attrToNamespaceUri = attrToNamespaceUri;
	}

    public AbstractPullReader createStaxReader(XMLStreamReader in) {
        return new AttrStaxReader(getQnameMap(), attrToNamespaceUri, in, xmlFriendlyReplacer());
    }

}
